using System;
using NUnit.Framework;
using Rhino.Mocks;

namespace noug.tdd.tests {
    [TestFixture]
    public class When_validating_user {
        private EmailValidator _emailValidator;
        UserValidator _userValidator;
        User _user;
        [SetUp]
        public void SetUp() {
            _emailValidator = MockRepository.GenerateStub<EmailValidator>();
            _userValidator = new UserValidator(_emailValidator);
            _user = new User() { Name = "George", Email = "a@b.c", Address = "" };
        }
        [Test]
        public void if_name_is_blank_return_false() {
            _user.Name = "";
            _emailValidator.Stub(x => x.IsValid(null)).IgnoreArguments().Return(true);
            Assert.IsFalse(_userValidator.IsValid(_user));
		}
        [Test]
        public void if_name_is_null_return_false() {
            _user.Name = null;
            _emailValidator.Stub(x => x.IsValid(null)).IgnoreArguments().Return(true);
            Assert.IsFalse(_userValidator.IsValid(_user));
        }
        [Test]
        public void if_address_is_null_return_false() {
            _user.Address = null;
            _emailValidator.Stub(x => x.IsValid(null)).IgnoreArguments().Return(true);
            Assert.IsFalse(_userValidator.IsValid(_user));
        }
        [Test]
        public void if_name_is_blank_dont_call_email_validator() {
            _user.Name = "";
            _userValidator.IsValid(_user);
            _emailValidator.AssertWasNotCalled(x => x.IsValid(_user.Email));
        }
        [Test]
        public void if_email_is_invalid_return_false() {
            _emailValidator.Stub(x => x.IsValid(null)).IgnoreArguments().Return(false);
            Assert.IsFalse(_userValidator.IsValid(_user));
        }
        [Test]
        public void if_email_is_valid_and_name_exists_return_true() {
            _emailValidator.Stub(x => x.IsValid(null)).IgnoreArguments().Return(true);
            Assert.IsTrue(_userValidator.IsValid(_user));
        }
    }
}
